<template>
  <BasicModal width="450px" v-bind="$attrs" title="批量设置考核官" @register="registerModal" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner } from '@/components/Modal'
import { BasicForm, useForm } from '@/components/Form'
import { computed } from '@vue/runtime-core';
import { batchUpdateAssessmentUser } from '@/api/train/detail.ts'
import { message } from 'ant-design-vue'

const props = defineProps({
  options: {
    type: Array,
    default: () => {
      return []
    },
  },
  selectList:{
    type: Array,
    default: () => {
      return []
    },
  },
  trainingNumberId:{
    type: [Number,String],
    default: ''
  }
})

const managementOptions = computed(()=>{
    return props.options
})


const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
  labelWidth: 80,
  baseColProps: { span: 24 },
  schemas: [
    {
      field: 'assessUserIds',
      label: '考核官：',
      component: 'Select',
      componentProps: {
        options: managementOptions,
        mode: 'multiple'
      },
      rules: [
        {
          required: true,
          message: '请选择考核官',
        },
      ],
    },
  ],
  showActionButtonGroup: false,
})

const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields()
  setModalProps({ confirmLoading: false })
})

const emit = defineEmits(['success', 'register'])
async function handleSubmit() {
    const values = await validate()
    setModalProps({ confirmLoading: true })
    try {
      const { assessUserIds } = values
      let params = {
        assessUserIds, // 考核官ids
        resumesIds: props.selectList.map((v:any) => v.resumesId),   // 简历id
        trainingNumberId: props.trainingNumberId
      }
      const res = await batchUpdateAssessmentUser(params)
      if(res.code == '10000'){
        emit('success', values)
        closeModal()
      }else{
        message.error(res.msg)
      }
    } finally {
      setModalProps({ confirmLoading: false })
      setModalProps({ confirmLoading: false })
    }
  }
</script>